package com.bestbuy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.FacebookProfile;
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bestbuy.pojo.Account;
import com.bestbuy.pojo.AccountFacebookInfo;
import com.bestbuy.pojo.Catalog;
import com.bestbuy.pojo.Product;
import com.bestbuy.service.AccountService;
import com.bestbuy.service.CatalogService;
import com.bestbuy.service.ProductService;
import com.bestbuy.util.APIWrapper;

@Controller
public class LoginController 
{
	@Autowired
	private AccountService accountService;
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private ProductService productService;
	static Logger log = Logger.getLogger(LoginController.class);
	
	
	@RequestMapping(value = { "/login.do" })
	public String LoginAccount(ModelMap model) 
	{
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		log.info("Hello World!!!");
		model.addAttribute("Page", "AccountLogin");
        
		return "Main";
	}

	@RequestMapping(value = { "/loginfail.do" })
	public String LoginAccountFail(ModelMap model) 
	{
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		model.addAttribute("error","Username or Password is not valid!");
		model.addAttribute("Page", "AccountLogin");
		return "Main";
	}
	
	@RequestMapping(value = { "/facebook-callback.do"}, method = RequestMethod.GET)
	public String LoginFacebook(Model model, HttpServletRequest request, HttpSession session) 
	{
		// cai nay de tra ve trang Index
//		List<Catalog> listCatalog = catalogService.getListCatalog();
//		model.addAttribute("catalogs", listCatalog);
//		List<Product> listProduct = productService.Search("", "0", "1");
//		model.addAttribute("products", listProduct);
//		model.addAttribute("isIndex", "SlideShow");
		
		// chuyen code thanh accessToken
		String code = request.getParameter("code");
		APIWrapper api = new APIWrapper();
		String accessToken = api.getAccessToken(code);
		// luu accessToken lai
		session.setAttribute("token", accessToken);
		api.setAccessToken(accessToken);
		// co accessToken => dung facebook API lay thong tin
		Facebook facebook = new FacebookTemplate(accessToken);
		// lay thong tin
		FacebookProfile profile = facebook.userOperations().getUserProfile();
		// luu thong tin xuong
		String username = profile.getUsername();
		String gender = profile.getGender();
		String fullname = profile.getName();
		String linkFacebook = profile.getLink();
		Account account = accountService.RegisterAccountWithFacebook(username, fullname, gender, linkFacebook);
		// luu account vao session
		session.setAttribute("username", username);
		// luu session nhan biet la Facebook
		session.setAttribute("isAccountFacebook", "isAccountFacebook");
		return "redirect:/index.do";
	}
	
	

}
